import java.util.Arrays;

public class O53 {
    public int search(int[] nums, int target) {
        int index = Arrays.binarySearch(nums,target);
        if (index<0)
            return 0;
        int l = index-1;
        int r = index+1;
        int sum = 1;
        while (true){
            if (l>=0&&nums[l]==target) {
                sum++;
                l--;
            }else if (r<nums.length&&nums[r]==target){
                sum++;
                r++;
            }else break;
        }
        return sum;
    }
}
